package com.zqxx.examine.management.controller.page.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.common.utils.QuestionAdapter;
import com.zqxx.examine.management.service.ExamPaperService;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.model.exam.ExamPaper;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.question.QuestionQueryResult;

@Controller
public class ExamPaperPageAdmin
{
  @Autowired
  private ExamPaperService examPaperService;
  @Autowired
  private QuestionService questionService;
  
  @RequestMapping(value={"/admin/exampaper/exampaper-list/{paperType}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examPaperListPage(Model model, HttpServletRequest request, @PathVariable("paperType") String paperType, @RequestParam(value="searchStr", required=false, defaultValue="") String searchStr, @RequestParam(value="page", required=false, defaultValue="1") int page)
  {
    Page<ExamPaper> pageModel = new Page();
    pageModel.setPageNo(page);
    pageModel.setPageSize(8);
    List<ExamPaper> paper = this.examPaperService.getExamPaperList(searchStr, paperType, pageModel);
    List<Field> fieldList = this.questionService.getAllField(null);
    
    String pageStr = PagingUtil.getPagelink(page, pageModel.getTotalPage(), "", "admin/exampaper/exampaper-list/" + paperType);
    model.addAttribute("fieldList", fieldList);
    model.addAttribute("paper", paper);
    model.addAttribute("pageStr", pageStr);
    model.addAttribute("searchStr", searchStr);
    return "exampaper-list";
  }
  
  @RequestMapping(value={"/admin/exampaper/exampaper-add"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examPaperAddPage(Model model, HttpServletRequest request)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    model.addAttribute("fieldList", fieldList);
    return "exampaper-add";
  }
  
  @RequestMapping(value={"/admin/exampaper/exampaper-edit/{exampaperId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examPaperEditPage(Model model, HttpServletRequest request, @PathVariable int exampaperId)
  {
    String strUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/";
    
    ExamPaper examPaper = this.examPaperService.getExamPaperById(exampaperId);
    StringBuilder sb = new StringBuilder();
    if ((examPaper.getContent() != null) && (!examPaper.getContent().equals("")))
    {
      Gson gson = new Gson();
      List<QuestionQueryResult> questionList = (List)gson.fromJson(examPaper.getContent(), new TypeToken() {}.getType());
      for (QuestionQueryResult question : questionList)
      {
        QuestionAdapter adapter = new QuestionAdapter(question, strUrl);
        sb.append(adapter.getStringFromXML());
      }
    }
    model.addAttribute("htmlStr", sb);
    model.addAttribute("exampaperid", Integer.valueOf(exampaperId));
    model.addAttribute("exampapername", examPaper.getName());
    return "exampaper-edit";
  }
  
  @RequestMapping(value={"/admin/exampaper/exampaper-preview/{examPaperId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  private String examPaperPreviewPage(Model model, HttpServletRequest request, @PathVariable int examPaperId)
  {
    String strUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/";
    
    ExamPaper examPaper = this.examPaperService.getExamPaperById(examPaperId);
    StringBuilder sb = new StringBuilder();
    if ((examPaper.getContent() != null) && (!examPaper.getContent().equals("")))
    {
      Gson gson = new Gson();
      List<QuestionQueryResult> questionList = (List)gson.fromJson(examPaper.getContent(), new TypeToken() {}.getType());
      for (QuestionQueryResult question : questionList)
      {
        QuestionAdapter adapter = new QuestionAdapter(question, strUrl);
        sb.append(adapter.getStringFromXML());
      }
    }
    model.addAttribute("htmlStr", sb);
    model.addAttribute("exampaperid", Integer.valueOf(examPaperId));
    model.addAttribute("exampapername", examPaper.getName());
    return "exampaper-preview";
  }
}
